package com.zhangshitong.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;

import com.zhangshitong.common.RequestHolder;
import com.zhangshitong.model.SysUser;

@Slf4j
public class LoginFilter implements Filter{

	@Override
	public void destroy() {
		
		
	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest req =(HttpServletRequest) servletRequest;
		HttpServletResponse resp =(HttpServletResponse)servletResponse;
		
		SysUser sysUser = (SysUser)req.getSession().getAttribute("user");//session中的这个user属性，是在用户登录的时候UserController中放到session中的
		if(sysUser == null){
			String path = "/signin.jsp";
			resp.sendRedirect(path);
			return;
		}
			RequestHolder.add(sysUser);
			RequestHolder.add(req);
			filterChain.doFilter(servletRequest, servletResponse);
			return;
		
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
		
	}

}
